﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;

namespace Paint
{
    public class VietChu
    {
        private int _FontSize;

        public int FontSize
        {
            get { return _FontSize; }
            set { _FontSize = value; }
        }
        private string _FontName;

        public string FontName
        {
            get { return _FontName; }
            set { _FontName = value; }
        }

        private Font _Font;

        public Font Font
        {
            get { return _Font; }
            set { _Font = value; }
        }


        //draw Point
        public Point pTopLeft;
        public Point pBottomLeft;
        public Point pTopRight;
        public Point pBottomRight;
        public Point pTop;
        public Point pBottom;
        public Point pLeft;
        public Point pRight;

        //draw rec
        public Rectangle r;
        private Pen p;

        //draw text
        public string key="";
       
        public SolidBrush br;

        //draw text finish
        public int flag = 2;

        public Point PointStart1 = new Point();
        public Point PointEnd1 = new Point();
        public Point PointStart2 = new Point();
        public Point PointEnd2 = new Point();
        public Size size = new Size();
        public int flagMain = 0;
        public int PanelWith;
        float[] dashvalue = {4,4};

        public delegate void FinishDrawText();
        public event FinishDrawText FinishDrawTextEvent;

        public VietChu()
        {
            p = new Pen(Color.Black) { DashPattern = dashvalue };
            FontSize = 8;
            this._Font = new Font(FontName, FontSize);
            br = new SolidBrush(Color.Black);
        }

        public void ChangeSize(VietChu vc,int FontSize)
        {
            vc.FontSize = FontSize;
            vc.Font=new Font(vc.FontName,vc.FontSize);
        }

        public void ChangeFont(VietChu vc, string FontName)
        {
            vc.FontName = FontName;
            vc.Font=new Font(vc.FontName,vc.FontSize);
        }

        public void ChangeColor(VietChu vc, Color c)
        {
            vc.br = new SolidBrush(c);
            if (vc.flag == 3)
            {
                FinishDrawTextEvent();
            }
        }

        public void DrawPoint(Graphics g)
        {
            pTopLeft = r.Location;

            pTopRight.X = r.Right;
            pTopRight.Y = r.Top;

            pBottomLeft.X = r.Left;
            pBottomLeft.Y = r.Bottom;

            pBottomRight.X = r.Right;
            pBottomRight.Y = r.Bottom;

            pTop.X = (r.Right + r.Left) / 2;
            pTop.Y = r.Top;

            pLeft.X = r.Left;
            pLeft.Y = (r.Bottom + r.Top) / 2;

            pBottom.X = (r.Right + r.Left) / 2;
            pBottom.Y = r.Bottom;

            pRight.X = r.Right;
            pRight.Y = (r.Bottom + r.Top) / 2;

            g.DrawRectangle(p, pTopLeft.X, pTopLeft.Y, 1, 1);
            g.DrawRectangle(p, pTopRight.X, pTopRight.Y, 1, 1);
            g.DrawRectangle(p, pBottomLeft.X, pBottomLeft.Y, 1, 1);
            g.DrawRectangle(p, pBottomRight.X, pBottomRight.Y, 1, 1);
            g.DrawRectangle(p, pTop.X, pTop.Y, 1, 1);
            g.DrawRectangle(p, pLeft.X, pLeft.Y, 1, 1);
            g.DrawRectangle(p, pBottom.X, pBottom.Y, 1, 1);
            g.DrawRectangle(p, pRight.X, pRight.Y, 1, 1);
        }

        public void SetRec(Point PointStart, Point PointEnd)
        {
            r = new Rectangle( Math.Min(PointStart.X,PointEnd.X),  Math.Min(PointStart.Y,PointEnd.Y), size.Width, size.Height);
        }

        public void Draw(Graphics g)
        {
            //draw end
            if (flag == 1)
            {
                string keyTemp = key;
                Font fontTemp = Font;
                SolidBrush brTemp = br;
                size = FixedSize(PanelWith, FontSize, PointStart1, PointEnd1);
                SetRec(PointStart1, PointEnd1);
                g.DrawString(keyTemp, fontTemp, brTemp, r.Location);
                key = "";
            }
            //draw next
            else if(flag==2)
            {
                size.Width = Math.Abs(PointStart1.X - PointEnd1.X);
                size.Height = Math.Abs(PointStart1.Y - PointEnd1.Y);
                p.Color = Color.Black;
                SetRec(PointStart1, PointEnd1);
                g.DrawRectangle(p, r);
            }
            else if (flag == 3)
            {
                size = FixedSize(PanelWith, FontSize, PointStart1, PointEnd1);                
                SetRec(PointStart1, PointEnd1);
                p.Color = Color.FromArgb(0x00, 0xCC, 0xFF);
                g.DrawRectangle(p, r);

                g.DrawString(key, Font, br, r);
                
            }
            else if (flag == 4)
            {
                size = FixedSize(PanelWith, FontSize, PointStart1, PointEnd1);
                p.Color = Color.FromArgb(0x00, 0xCC, 0xFF);
                SetRec(PointStart1, PointEnd1);
                g.DrawRectangle(p, r);

                g.DrawString(key, Font, br, r);

                size.Width = Math.Abs(PointStart2.X - PointEnd2.X);
                size.Height = Math.Abs(PointStart2.Y - PointEnd2.Y);
                p.Color = Color.Black;
                SetRec(PointStart2, PointEnd2);
                g.DrawRectangle(p, r);               
            }
        }

        public Size FixedSize(int _PanelWidth, int FontSize, Point PointStart, Point PointEnd)
        {
            size.Width = Math.Abs(_PanelWidth - 1 - Math.Min(PointStart.X,PointEnd.X));
            size.Height = Math.Max(Math.Abs(PointEnd.Y - PointStart.Y), FontSize * 2);
            return size;
        }
    }
}
